/*
 * @Description: 
 * @Author: rendc
 * @Date: 2024-10-11 10:54:37
 * @LastEditors: rendc
 * @LastEditTime: 2024-10-11 11:14:13
 */

/**
 * Printer
 */
public class Printer {

  // 普通（非静态）的同步方法
  // 普通同步方法的锁：默认的锁对象是 this
  public synchronized void print1() {
    System.out.print("天");
    System.out.print("天");
    System.out.print("向");
    System.out.print("上");
    System.out.println("");
  }

  Object mutex = new Object();

  // 不同步的方法
  public void print2() {
    // 同步代码块 - 要和同步方法使用同一把锁
    synchronized (this) {
      System.out.print("好");
      System.out.print("好");
      System.out.print("学");
      System.out.print("习");
      System.out.println("");
    }
  }

  // 静态同步方法 - 锁对象是 Printer.class
  public static synchronized void print3() {
    System.out.print("天");
    System.out.print("天");
    System.out.print("向");
    System.out.print("上");
    System.out.println("");
  }

  public void print4() {
    // 同步代码块 - 要和同步方法使用同一把锁
    synchronized (Printer.class) {
      System.out.print("好");
      System.out.print("好");
      System.out.print("学");
      System.out.print("习");
      System.out.println("");
    }
  }
}